package com.xyz.advice;

import com.xyz.entity.response.ResultResponse;
import com.xyz.enums.ResponseEnum;
import com.xyz.exception.IllegalUsernameOrPasswordException;
import com.xyz.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 夏永泽
 * @date 2022/4/1
 */
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(IllegalUsernameOrPasswordException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResultResponse<Object> handleIllegalUsernameOrPasswordException() {
        return ResultResponse.failed(ResponseEnum.FORBIDDEN);
    }

    @ExceptionHandler(IllegalAccessException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResultResponse<Object> handleIllegalAccessException() {
        return ResultResponse.failed(ResponseEnum.ILLEGAL_ACCESS);
    }

    @ExceptionHandler(UserNotExistException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResultResponse<Object> handleUserNotExistException() {
        return ResultResponse.failed(ResponseEnum.USER_NOT_EXIST);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultResponse<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return ResultResponse.failed(e.getAllErrors().get(0).getDefaultMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultResponse<Object> handleIllegalArgumentException(IllegalArgumentException e) {
        return ResultResponse.failed(ResponseEnum.ILLEGAL_ARGUMENT);
    }
}
